//
//  DataBaseHelper.m
//  qiuShiJiangHu
//
//  Created by xalo on 16/1/27.
//  Copyright © 2016年 糗事江湖项目组. All rights reserved.
//

#import "DataBaseHelper.h"
#import <FMDB/FMDB.h>
#import "HomePageModel.h"


static DataBaseHelper *handle = nil;
@interface DataBaseHelper ()

@property(nonatomic,strong)FMDatabase *db;

@end



@implementation DataBaseHelper


+(DataBaseHelper *)sharedManager {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        handle = [[DataBaseHelper alloc]init];
    });
    
    return handle;
}


- (instancetype)init {
    
    self = [super init];
    if (self) {
        NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
        NSString *dbPath = [documentPath stringByAppendingPathComponent:@"QiuShi.sqlite"];
        self.db = [FMDatabase databaseWithPath:dbPath];
        //打开数据库
        [self.db open];
        //创建表
        if ([self.db open]) {
            //SQL语句
            
            NSString *sql = @"create table 'QiuShiJiangHu' (ID integer primary key not null , model blob );";
            //创建表
            BOOL result = [self.db executeUpdate:sql];
            
            if (result) {
                NSLog(@"表创建成功");
            }
        }
        //关闭数据库
        [self.db close];
        
    }
    return self;
}


//添加到数据库
- (BOOL)insertToSqlitWithMode:(HomePageModel *)model ID:(NSInteger)ID{

    //打开数据库
    [self.db open];
    
    NSData *data = [DataBaseHelper archiverObject:model key:ID];
  
    //sql语句

    NSString *sql = [NSString stringWithFormat:@"insert into 'QiuShiJiangHu' values(%ld,?);",model.ID];
    //执行
    BOOL result = [self.db executeUpdate:sql,data];
    //关闭数据库
    [self.db close];
    return result;
}

//根据Id删除

- (BOOL)deleteWithID:(NSInteger)ID{
    
    //sql语句
    NSString *sql = [NSString stringWithFormat:@"delete from 'QiuShiJiangHu' where ID = %ld;",ID] ;
    //打开数据库
    [self.db open];
    //执行
    BOOL result = [self.db executeUpdate:sql];
    //关闭数据库
    [self.db close];
    
    return result;
    
}

//全部删除
- (BOOL)deleteAll {
    
    //sql语句
    NSString *sql = @"delete from 'QiuShiJiangHu';";
    //打开数据库
    [self.db open];
    //执行
    BOOL result = [self.db executeUpdate:sql];
    //关闭数据库
    [self.db close];
    
    return result;
}

//根据Id查找

- (HomePageModel *)selectWithID:(NSInteger)ID{
    
    HomePageModel *model = [[HomePageModel alloc] init];

    //打开数据库
    [self.db open];
    
    NSString *sql = [NSString stringWithFormat:@"select *from 'QiuShiJiangHu' where ID = %ld;",ID];
    
    FMResultSet *fmr = [self.db executeQuery:sql];
   
    while ([fmr next]) {

        NSData *data = [fmr dataForColumn:@"model"];
        NSInteger ID1 = [fmr intForColumn:@"ID"];
        //反归档
        model = [DataBaseHelper unarchiverW:data key:ID1];
    }
    [fmr close];
    
    //关闭数据库
    [self.db close];
    
    return model;
}

//查找全部
- (NSMutableArray *)selectAll {
    
    NSMutableArray *array = [NSMutableArray array];
    
    //打开数据库
    [self.db open];

    NSString *sql = @"select *from 'QiuShiJiangHu';";
    
    FMResultSet *fmr = [self.db executeQuery:sql];
    while ([fmr next]) {

        NSData *data = [fmr dataForColumn:@"model"];
        NSInteger ID2 = [fmr intForColumn:@"ID"];
        
        //反归档

      HomePageModel  *model = [DataBaseHelper unarchiverW:data key:ID2];

        [array addObject:model];
    }
    
    [fmr close];
    //关闭数据库
    [self.db close];
    
    return array;
}


//判断是否已经收藏,从数据库中获取
+ (BOOL)isCollectionHomePageModel:(HomePageModel *)model {
    
    BOOL isCollection = NO;
    
    HomePageModel *newModel = [[DataBaseHelper sharedManager] selectWithID:model.ID];
    
    if (newModel.ID == model.ID) {

        isCollection = YES;
    }
    return isCollection;
}


//归档
+(NSData *)archiverObject:(id)object key:(NSInteger)key
{
    // mutableData
    NSMutableData *data = [NSMutableData data];
    
    //创建archiver 对象
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    
    //归档
    [archiver encodeObject:object forKey:[NSString stringWithFormat:@"%ld",key]];
    
    //结束归档
    [archiver finishEncoding];
    
    //返回
    return data;
}

//反归档
+(id)unarchiverW:(NSData *)data key:(NSInteger)key
{
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
  
    id object = [unarchiver decodeObjectForKey:[NSString stringWithFormat:@"%ld",key]];
    
    [unarchiver finishDecoding];
    
    return object;
}








@end
